package com.bw.ppjokeplay.mvvm.viewmodel.find

import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.ppjokeplay.R
import com.bw.ppjokeplay.adapter.FindConcernAdapter
import com.bw.ppjokeplay.adapter.FindRecommendAdapter
import com.bw.ppjokeplay.commen.entity.BaseEntity
import com.bw.ppjokeplay.commen.mvvm.viewmodel.BaseViewModel
import com.bw.ppjokeplay.mvvm.model.entity.AddTagFollowEntity
import com.bw.ppjokeplay.mvvm.model.entity.TagDataEntity
import com.bw.ppjokeplay.mvvm.model.entity.TagEntity
import com.bw.ppjokeplay.mvvm.model.find.FindRecommendModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/13 0013 上午 8:41
 *
 */
class FindRecommendViewModel: BaseViewModel<FindRecommendModel>(),OnItemChildClickListener {
    var offset:Int=0
    var pageCount:Int=10
    var tagId:Int=0
    var tagType:String="all"
    var userId:Int=1
    var data: ArrayList<TagDataEntity>? = null
    var Adapter : FindRecommendAdapter = FindRecommendAdapter()
    override fun initdata() {
        requesttj()
        Adapter.setOnItemChildClickListener(this)
    }
    fun requesttj(){
        m.requestrecommendList(offset, pageCount, tagId, tagType, userId)!!
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this)
    }
    override fun result(t: BaseEntity) {
        if (t is TagEntity){
            val tagEntity = t as TagEntity
            LogUtils.e(tagEntity.data!!.data)
            data = tagEntity.data!!.data
            Adapter.setNewInstance(tagEntity.data!!.data)
        }else if (t is AddTagFollowEntity){
            val addTagFollowEntity = t as AddTagFollowEntity
            if (addTagFollowEntity.data!!.data!!.hasFollow ==true){
                ToastUtils.showLong("关注成功")
                requesttj()
            }else if (addTagFollowEntity.data!!.data!!.hasFollow ==false){
                ToastUtils.showLong("取消关注")
                requesttj()
            }

        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when(view.id){
            R.id.action_follow->{
                m.requestAddTagList( data!!.get(position).tagId,  1)!!
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this)

            }

        }
    }

}